﻿using SGA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SGA.Charges
{
    public partial class Charge : System.Web.UI.Page
    {
        private const int K_CHARGE_TAB = 0;
        private const int K_BANK_TAB = 1;
        private const int K_DAY_TAB = 2;

        protected void Page_Load(object sender, EventArgs e)
        {
            NotificationLabel.Text = string.Empty;

            if (!IsPostBack)
            {
                //Esta a preencher os dias de cobrança por cada instancia de cobrança (tabulador)
                ChargeTabContainer_ActiveTabChanged(ChargeTabContainer, null);
                FillAnoDdl();
                MonthDdl.SelectedValue = DateTime.Now.Month.ToString();
                DataCollectionTbox.Text = DateTime.Today.ToString("dd-MM-yyyy");
            }

        }


        /// <summary>
        /// Preenche a dropDownList do ano
        /// </summary>
        private void FillAnoDdl()
        {
            int year = DateTime.Now.Year;
            int value = 0;

            for (int i = 0; i < 15; i++)
            {
                value = year + i - 10;
                YearDdl.Items.Add(value.ToString());
            }
            YearDdl.SelectedValue = year.ToString();
        }


        protected void ChargeTabContainer_ActiveTabChanged(object sender, EventArgs e)
        {
            try
            {
                switch (ChargeTabContainer.ActiveTabIndex)
                {
                    case K_CHARGE_TAB:
                        using (var bd = new MailingEntities())
                        {
                            //bancos activos
                            BankDdl.DataSource = (from banks in bd.CollectingBanks
                                                  where banks.IsActive == true
                                                  orderby banks.Designation
                                                  select banks).ToList();
                            BankDdl.DataBind();

                            //banco padrão
                            var defaultBank = (from bank in bd.CollectingBanks
                                               where bank.IsDefault == true
                                               select bank.Id).SingleOrDefault();
                            BankDdl.SelectedValue = defaultBank.ToString();

                            //dias de cobrança (faixas) activas
                            RangeCollectionDdl.DataSource = (from days in bd.CollectingRanges
                                                             where days.IsActive == true
                                                             orderby days.CollectingDay
                                                             select days).ToList();
                            RangeCollectionDdl.DataBind();
                        }
                        break;

                    case K_BANK_TAB:
                        //todos os bancos de cobrança
                        CollectionBanksLvw.DataSourceID = "EDSCollectionBanks";
                        CollectionBanksLvw.DataBind();
                        break;

                    case K_DAY_TAB:
                        //todos os dias (faixas) de cobrança
                        CollectionRangeLvw.DataSourceID = "EDSCollectonRanges";
                        CollectionRangeLvw.DataBind();
                        break;

                    default:
                        throw new Exception("Opção desconhecida! Informe o programador.");
                }
            }
            catch (Exception ex)
            {
                NotificationLabel.Text = ex.Message.ToString();
            }
        }


        /// <summary>
        /// Cria o ficheiro de cobranças no formato SEPA
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreateSepaFileBtn_Click(object sender, EventArgs e)
        {

        }

        private bool ValidValueControls()
        {
            if (!FirstRbtn.Checked && !RcurRbtn.Checked)
                return false;

            if (RangeCollectionDdl.SelectedValue.Equals(string.Empty))
                return false;

            return true;
        }

        /// <summary>
        /// Cria uma nova emissão de cobranças
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void NewCollectionBtn_Click(object sender, EventArgs e)
        {
            if (!ValidValueControls())
                return;


        }
    }
}